import util from '../../../utils/index.js';
import { infoModel, fileModel, categoryModel } from '../../../apis/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: [
    {
      style: "redirect",
      key: 'categoryid',
      title: "行业",
      placeholder: "选择行业",
      must: true
    }, {
      style: "text",
      key: 'address',
      title: "职位",
      placeholder: "填写职位",
    }, {
      style: "number",
      key: 'phone',
      title: "电话",
      placeholder: "填写11位手机号码",
    }, {
      style: "address",
      key: 'phone',
      title: "地址",
      placeholder: "选择详细地址",
    }, {
      style: "redirect",
      key: 'phone',
      title: "商户详情",
      placeholder: "填写文字图片等介绍",
    }],
    param: {
      img: [],
      categoryid: "",
      address: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  chooseImage: function (e) {
    let that = this;
    let { param } = that.data;
    let { key, size } = util.getData(e);
    let imageList = param[key] || [];
    key = `param.${key}`;

    wx.chooseImage({
      count: size - imageList.length,
      success: function (res) {

        let tempFiles = res.tempFiles;
        //定义图片id数组
        let imageIdList = [];
        // 格式化图片参数

        for (let i = 0; i < tempFiles.length; i++) {
          fileModel.upload({ filePath: tempFiles[i].path }).then((d) => {
            tempFiles[i].id = d;
            imageIdList.push(d);
            //检验传的照片是否重复
            let repeat = imageList.findIndex((item) => {
              return item.id == tempFiles[i].id
            }) < 0 ? false : true;
            if (!repeat) {
              tempFiles[i].status = 1
              imageList = [...imageList, tempFiles[i]];
            }

            that.setData({
              [key]: imageList
            })
            util.hideAll()
          }).catch((d) => {

          })
        }

      },
    })
  },
  delImage: function (e) {
    let { index, key } = util.getData(e);
    let { param } = this.data;
    let imageList = param[key];
    imageList.splice(index, 1);
    key = `param.${key}`;
    this.setData({
      [key]: imageList
    })
  },
})